package org.aplikator.server.rpc.handler;

import org.aplikator.client.rpc.impl.RunFunction;
import org.aplikator.client.rpc.impl.RunFunctionResponse;
import org.aplikator.server.Context;
import org.aplikator.server.DescriptorRegistry;
import org.aplikator.server.descriptor.Function;
import org.aplikator.server.function.FunctionParameters;
import org.aplikator.server.function.FunctionResult;
import org.aplikator.server.rpc.CommandHandler;

public class RunFunctionHandler implements CommandHandler<RunFunction, RunFunctionResponse> {

    public RunFunctionHandler() {

    }

    public RunFunctionResponse execute(RunFunction command, Context context) {
        Function function = (Function) DescriptorRegistry.get().getDescriptionItem(command.getFunction().getId());
        FunctionResult result = function.getExecutable().execute(new FunctionParameters(command.getClientContext(), command.getFunctionParameters()), context);
        return new RunFunctionResponse(result != null ? result.getMessage() : "");
    }

}
